package com.changhong.autopairservice.core;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.changhong.autopairservice.utils.Common;
import com.changhong.autopairservice.utils.ToastUtil;

//by: yazhou.zhu@changhong.com
public class MainReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("android.intent.action.BOOT_COMPLETED".equals(action) || "android.intent.action.REBOOT".equals(action)) {
            // For newer Android versions, we should start a foreground service or use JobScheduler
            // But for simplicity, we'll start the service anyway and let it handle Bluetooth state
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // On Android 8.0+, use startForegroundService
                context.startForegroundService(new Intent(context, MainService.class));
                Common.log("Start automatic pairing service when booting (Android 8.0+)");
            } else {
                context.startService(new Intent(context, MainService.class));
                Common.log("Start automatic pairing service when booting");
            }
        } else if ("android.bluetooth.adapter.action.STATE_CHANGED".equals(action)) {
            switch (intent.getIntExtra("android.bluetooth.adapter.extra.STATE", -1)) {

                case BluetoothAdapter.STATE_OFF /*10*/:
                case BluetoothAdapter.STATE_TURNING_OFF /*13*/:
                    Common.log("Stop the automatic pairing service when the Bluetooth adapter is turned off");
                    ToastUtil.getInstance(context).showErrString("蓝牙已关闭");
                    context.stopService(new Intent(context, MainService.class));
                    return;
                case BluetoothAdapter.STATE_ON /*12*/:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        context.startForegroundService(new Intent(context, MainService.class));
                    } else {
                        context.startService(new Intent(context, MainService.class));
                    }
                    ToastUtil.getInstance(context).showErrString("蓝牙已开启");
                    Common.log("start automatic pairing service when the Bluetooth adapter is turned on");
                    return;
                default:
                    return;
            }
        } else if (AutoPairManager.ACTION_CANCEL_AUTO_PAIR.equals(action)) {
            Common.log("Cancel automatic pairing service!!!!!!");
            context.stopService(new Intent(context, MainService.class));
            // 移除System.exit(0)调用，改为更安全的方式
        } else if (AutoPairManager.ACTION_START_AUTO_PAIR.equals(action)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(new Intent(context, MainService.class));
            } else {
                context.startService(new Intent(context, MainService.class));
            }
            Common.log("Start automatic pairing service!!!!!!");
        } else {
            Common.log("Stop automatic pairing service when shutting down");
            context.stopService(new Intent(context, MainService.class));
            // 移除System.exit(0)调用，改为更安全的方式
        }
    }
}